//
// Snake.cpp for Snake.cpp in /home/titouan/Dropbox/Code/nibbler-epitech-2017
//
// Made by Titouan Creach
// Login   <titouan@epitech.net>
//
// Started on  Sat Mar 22 19:16:47 2014 Titouan Creach
// Last update Mon Mar 24 14:06:20 2014 Titouan Creach
//

#include <cstdlib>
#include "Snake.hpp"

Snake::Snake(int w, int h)
{
  Point p;

  p.x = ((w / 16) / 2);
  p.y = ((h / 16) / 2);
  _pointList.push_back(p);
  _actualDir = RIGHT;
  _hasEaten = 3;
}

void Snake::addNode()
{
  int nb = rand() % 3;
  _hasEaten += (nb + 1);
}

void Snake::move(Snake::Direction d)
{
  if (_actualDir == LEFT && d == RIGHT)
    return;
  if (_actualDir == RIGHT && d == LEFT)
    return;
  if (_actualDir == UP && d == DOWN)
    return;
  if (_actualDir == DOWN && d == UP)
    return;
  _actualDir = d;
}

std::size_t Snake::getScore() const
{
  return (_pointList.size() >= 4) ? _pointList.size() - 4 : 0;
}

void Snake::update()
{

  Point p = _pointList[0];

  switch (_actualDir)
    {
    case LEFT:
      p.x--;
      break;

    case RIGHT:
      ++p.x;
      break;

    case DOWN:
      ++p.y;
      break;

    case UP:
      --p.y;
      break;
    }

  if (_hasEaten > 0)
    {
      --_hasEaten;
    }
  else
    {
      _pointList.pop_back();
    }
  _pointList.push_front(p);
}
